<template>
  <el-dialog
    title="闸门控制"
    :close-on-click-modal="false"
    width="500px"
    :visible.sync="visible">
    <el-form v-loading="formLoading" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="闸门1" prop="gateA">
      <el-radio-group v-model="dataForm.gateA">
        <el-radio label="0">关</el-radio>
        <el-radio label="1">开</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="闸门2" prop="gateB">
      <el-radio-group v-model="dataForm.gateB">
        <el-radio label="0">关</el-radio>
        <el-radio label="1">开</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="闸门3" prop="gateC">
      <el-radio-group v-model="dataForm.gateC">
        <el-radio label="0">关</el-radio>
        <el-radio label="1">开</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="闸门4" prop="gateD">
      <el-radio-group v-model="dataForm.gateD">
        <el-radio label="0">关</el-radio>
        <el-radio label="1">开</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="闸门5" prop="gateE">
      <el-radio-group v-model="dataForm.gateE">
        <el-radio label="0">关</el-radio>
        <el-radio label="1">开</el-radio>
      </el-radio-group>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-loading="formLoading" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        formLoading: false,
        dataForm: {
          farmlandInfoId:'',
          gateA: 0,
          gateB: 0,
          gateC: 0,
          gateD: 0,
          gateE: 0,
        },
        dataRule: {
          gateA: [
            { required: true, message: '闸门1不能为空', trigger: 'blur' }
          ],
          gateB: [
            { required: true, message: '闸门2不能为空', trigger: 'blur' }
          ],
          gateC: [
            { required: true, message: '闸门3不能为空', trigger: 'blur' }
          ],
          gateD: [
            { required: true, message: '闸门4不能为空', trigger: 'blur' }
          ],
          gateE: [
            { required: true, message: '闸门5不能为空', trigger: 'blur' }
          ],
                
        }
      }
    },
    methods: {
      init (id) {
        this.visible = true
        this.formLoading = false
        this.dataForm.farmlandInfoId=id
        this.$http({
            url: this.$http.adornUrl('/buss/bussrtusendrecord/getByTkId'),
            method: 'get',
            params: this.$http.adornParams({
              'tkId':id
          })
        }).then(({data}) => {     
            if(data.xxbw!=undefined){
              console.log(data.xxbw)
              this.dataForm.gateA=data.xxbw.gateA
              this.dataForm.gateB=data.xxbw.gateB
              this.dataForm.gateC=data.xxbw.gateC
              this.dataForm.gateD=data.xxbw.gateD
              this.dataForm.gateE=data.xxbw.gateE
            }else{
              this.dataForm.gateA='0'
              this.dataForm.gateB='0'
              this.dataForm.gateC='0'
              this.dataForm.gateD='0'
              this.dataForm.gateE='0'
            }
            
        }) 
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid && !this.formLoading) {
            this.formLoading = true
            this.$http({
              url: this.$http.adornUrl(`/buss/bussrtusendrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'farmlandInfoId':this.dataForm.farmlandInfoId,
                'gateA': this.dataForm.gateA,
                'gateB': this.dataForm.gateB,
                'gateC': this.dataForm.gateC,
                'gateD': this.dataForm.gateD,
                'gateE': this.dataForm.gateE,               
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.formLoading = false
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
